package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Orders;
import com.example.domain.other.Address;
import com.example.domain.other.OrderDetail;
import com.example.dto.OrderAddressDetailDto;
import com.example.result.JsonResult;
import com.example.service.AddressBookService;
import com.example.service.OrderDetailService;
import com.example.service.OrderService;
import com.example.untils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *   订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService ordersService;
    @Autowired
    private AddressBookService addressService;
    @Autowired
    private OrderDetailService orderDetailService;

    //获取订单列表
    @GetMapping("/userPage")
    public JsonResult userPage(Integer page,Integer pageSize){
        Long currentId = BaseContext.getCurrentId();
        log.info("用户id:"+currentId);
        IPage<Orders> iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(currentId!=null, Orders::getUserId,currentId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(iPage,queryWrapper);
        return new JsonResult(1,"",iPage);
    }
    //获取订单详情分页
    @GetMapping("/page/{currentPage}/{pageSize}")
    public JsonResult page(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Long id,String startTime,String endTime){
        //System.out.println(toString());
        IPage<Orders> iPage=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(id!=null,Orders::getId,id);
        queryWrapper.ge(!Strings.isEmpty(startTime),Orders::getOrderTime,startTime);     //集合中下标0为开始时间
        queryWrapper.lt(!Strings.isEmpty(endTime),Orders::getOrderTime,endTime);     //集中中下标1为结束时间
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(iPage,queryWrapper);

        return new JsonResult(1,"",iPage);
    }
    //设置状态
    @GetMapping("/setStatus/{id}/{status}")
    public JsonResult page(@PathVariable Long id,@PathVariable Integer status){
        LambdaUpdateWrapper<Orders> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId,id);
        updateWrapper.set(Orders::getStatus,status+1);
        boolean flag = ordersService.update(updateWrapper);
        if(!flag){
            return new JsonResult(0,"更新失败,请重试");
        }

        return new JsonResult(1,"更新成功");
    }
    //查看订单详情
    @PostMapping("/detail/{id}")
    public JsonResult detail(@PathVariable Long id){
        if(id==null){
            return new JsonResult(0,"获取错误!");
        }
        Orders order = ordersService.getById(id);
        if(order==null){
            return new JsonResult(0,"信息不存在,请重试!");
        }
        LambdaQueryWrapper<Address> addressWrapper=new LambdaQueryWrapper<>();
        addressWrapper.eq(Address::getId,order.getAddressId());
        Address address = addressService.getOne(addressWrapper);

        LambdaQueryWrapper<OrderDetail> orderDetailWrapper=new LambdaQueryWrapper<>();
        orderDetailWrapper.eq(OrderDetail::getOrderId,order.getId());
        List<OrderDetail> list = orderDetailService.list(orderDetailWrapper);

        OrderAddressDetailDto orderAddressDetailDto=new OrderAddressDetailDto();
        orderAddressDetailDto.getByOAD(order,address,list);
        return new JsonResult(1,"",orderAddressDetailDto);
    }
}
